<script setup>
import { defineProps, defineEmits, reactive, ref } from 'vue'
import * as API from '@/api'
import moment from 'moment'

const state = reactive({
  // 有id就是修改, 没有 id 就是创建
  form: {
    applyType: 1,
    startTime: '',
    endTime: '',
    leaveTime: '0天',
    reasons: ''
  }
})

const { showModel } = defineProps({
  showModel: Boolean
})

const dialogForm = ref()
const emits = defineEmits(['closed'])

const reset = () => {
  state.form = {
    applyType: 1,
    startTime: '',
    endTime: '',
    leaveTime: '0天',
    reasons: ''
  }
}

const handelClosed = () => {
  emits('closed')
  reset()
}

const handelSubmit = async () => {
  dialogForm.value.validate( async (valid) => {
    if (valid) {
      try {
        let res = await API.leaveOperate(state.form)
        emits('closed')
        reset()
      } catch (error) {
      }
    }
  })
}

const handelDateChange = () => {
  const { startTime, endTime }  = state.form
  if (startTime && endTime) {
    state.form.leaveTime = `${((endTime - startTime) / 1000 / 60 / 60 / 24) + 1} 天`
  } else {
    state.form.leaveTime = '0天'
  }
}

const rules = {
  applyType: [
    { required: true, message: '请选择请假类型', trigger: 'blur' },
  ],
  startTime: [
    { required: true, message: '请选择开始日期', trigger: 'blur' },
  ],
  endTime: [
    { required: true, message: '请选择结束日期', trigger: 'blur' },
  ],
  reasons: [
    { required: true, message: '请输入事由', trigger: 'blur' },
  ]
}

</script>
<template>
  <el-dialog title="休假申请" :model-value="showModel" @closed="handelClosed">
    <el-form ref="dialogForm" :model="state.form" label-width="120" :rules="rules">

      <el-form-item label="休假类型" prop="applyType" required>
        <el-select v-model="state.form.applyType" 
                placeholder="请选择请假类型">
          <el-option :value="1" label="事假" ></el-option>
          <el-option :value="2" label="调休" ></el-option>
          <el-option :value="3" label="年假" ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="休假申请">
        <el-row>
          <el-col :span="8">
            <el-form-item prop="startTime" required>
              <el-date-picker 
                v-model="state.form.startTime"
                type="date"
                placeholder="请选择开始日期"
                @change="handelDateChange"
              />
            </el-form-item>
          </el-col>
          <el-col :span="1" style="text-align: center;">
            -
          </el-col>
          <el-col :span="8">
            <el-form-item prop="endTime" required>
              <el-date-picker 
                v-model="state.form.endTime"
                type="date"
                placeholder="请选择结束日期"
                @change="handelDateChange"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="休假时长">
        {{ state.form.leaveTime }}
      </el-form-item>
      <el-form-item label="休假原因" prop="reasons" required>
        <el-input 
        type="textarea"
         rows="3" 
         v-model="state.form.reasons"
          placeholder="请选输入请假原因" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="handelClosed">取消</el-button>
      <el-button @click="handelSubmit" type="primary">确定</el-button>
    </template>

  </el-dialog>
</template>